var JobList={
   useUndo:true,
   do_save:function(jobs){
      if(!$.isArray(jobs)){
         jobs=[jobs];
      }

      var oldActions = [];
      var njobs = []; 
      $.each(jobs,function(index,job) {
         var saveAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .action.save');
         if(saveAction.length <= 0)
            return true;
         saveAction.tooltip('hide');
         oldActions[njobs.length] = saveAction[0].outerHTML;
         njobs[njobs.length++] = job; 
         saveAction[0].outerHTML = '<img class="loading" data-action="Save" src="'+ OC.imagePath('core', 'loading.gif') +'"></a>';
      });

      $.ajax({
         type: "POST",
         url: OC.filePath('jobposition', 'ajax', 'save.php'),
         data: {ids:JSON.stringify(njobs)},
         dataType: 'json',
         beforeSend: function(xhr, settings) {
            $('#jobActions .save').each(function(){
               if(!showLoading($(this), settings, null, {button:true}))
                  return false;
            });
         },
         success: function(result){
            if (result.status == 'success') {
               $.each(njobs,function(index,job){
                  var jobWrap = $('.jobitem').filterAttr('data-id',job);
                  /* in /All mode, change html only */
                  jobWrap.data('saved', 1).addClass('saved');
                  var saveAction = jobWrap.find('.jobactions .loading');
                  if(saveAction.length >0) {
                     /* now it's saved. change it to to saved */
                     var html = '<a href="#" class="action unsave" data-action="unsave" title="取消收藏">';
                     html += '</a>';
                     saveAction[0].outerHTML = html;
                     jobWrap.find('.jobactions .action.unsave').tooltip().data('action', 'Unsave');
                  }
               });
               procesSelection();
            } else {
               $.each(njobs,function(index,job) {
                  var saveAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .loading');
                  if(saveAction.length >0 && typeof(oldActions[index]) != 'undefined')
                     saveAction[0].outerHTML = oldActions[index];
               });
               OC.Notification.showHtml(result.data.message+'<span class="undo tclose">关闭</span>', 3000);
            }
         }
      });
   },

   do_unsave:function(jobs){
      if(!$.isArray(jobs)){
         jobs=[jobs];
      }

      var oldActions = [];
      var njobs = [];
      $.each(jobs,function(index,job) {
         var unsaveAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .action.unsave');
         if(unsaveAction.length <= 0)
            return true; 

         unsaveAction.tooltip('hide'); 
         oldActions[njobs.length] = unsaveAction[0].outerHTML;
         njobs[njobs.length++] = job; 
         unsaveAction[0].outerHTML = '<img class="loading" data-action="Unsave" src="'+ OC.imagePath('core', 'loading.gif') +'"></a>';
      });

      $.ajax({
         type: "POST",
         url: OC.filePath('jobposition', 'ajax', 'unsave.php'),
         data: {ids:JSON.stringify(njobs)},
         dataType: 'json',
         beforeSend: function(xhr, settings) {
            $('#jobActions .unsave').each(function(){
               if(!showLoading($(this), settings, null, {button:true}))
                  return false;
            });
         },
         success: function(result){
            if (result.status == 'success') {
               $.each(njobs,function(index,job){
                  var jobWrap = $('.jobitem').filterAttr('data-id',job);
                  if(jobWrap.data('type') == 'saved') {
                     jobWrap.remove();
                  } else {
                     /* in /All mode, change html only */
                     jobWrap.data('saved', 0).addClass('unsaved');
                     var unsaveAction = jobWrap.find('.jobactions .loading');
                     if(unsaveAction.length >0) {
                        /* now it's saved. change it to to saved */
                        var html = '<a href="#" class="action save" data-action="save" title="收藏">';
                        html += '</a>';
                        unsaveAction[0].outerHTML = html;
                        jobWrap.find('.jobactions .action.save').tooltip().data('action', 'Save');
                     }
                  }
               });
               procesSelection();
            } else {
               $.each(njobs,function(index,job) {
                  var unsaveAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .loading');
                  if(unsaveAction.length >0 && typeof(oldActions[index]) != 'undefined')
                     unsaveAction[0].outerHTML = oldActions[index];
               });
               OC.Notification.showHtml(result.data.message+'<span class="undo tclose">关闭</span>', 3000);
            }
         }
      })
   },
   
   do_settop:function(jobs){
      if(!$.isArray(jobs)){
         jobs=[jobs];
      }

      var oldActions = [];
      var njobs = []; 
      $.each(jobs,function(index,job) {
         var settopAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .action.settop');
         if(settopAction.length <= 0)
            return true;
         settopAction.tooltip('hide');
         oldActions[njobs.length] = settopAction[0].outerHTML;
         njobs[njobs.length++] = job; 
         settopAction[0].outerHTML = '<img class="loading" data-action="Settop" src="'+ OC.imagePath('core', 'loading.gif') +'"></a>';
      });

      $.ajax({
         type: "POST",
         url: OC.filePath('jobposition', 'ajax', 'settop.php'),
         data: {ids:JSON.stringify(njobs)},
         dataType: 'json',
         beforeSend: function(xhr, settings) {
            $('#jobActions .settop').each(function(){
               if(!showLoading($(this), settings, null, {button:true}))
                  return false;
            });
         },
         success: function(result){
            if (result.status == 'success') {
               $.each(njobs,function(index,job){
                  var jobWrap = $('.jobitem').filterAttr('data-id',job);
                  /* in /All mode, change html only */
                  jobWrap.data('settop', 1).addClass('settop');
                  var settopAction = jobWrap.find('.jobactions .loading');
                  if(settopAction.length >0) {
                     /* now it's settoped. change it to to settoped */
                     var html = '<a href="#" class="action unsettop" data-action="unsettop" title="点击取消置顶">';
                     html += '</a>';
                     settopAction[0].outerHTML = html;
                     jobWrap.find('.jobactions .action.unsettop').tooltip().data('action', 'Unsettop');
                  }
               });
               procesSelection();
               OC.Notification.showHtml('职位已置顶成功，页面重载后会显示到顶部'+'<span class="undo tclose">关闭</span>', 5000);
            } else {
               $.each(njobs,function(index,job) {
                  var settopAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .loading');
                  if(settopAction.length >0 && typeof(oldActions[index]) != 'undefined')
                     settopAction[0].outerHTML = oldActions[index];
               });
               OC.Notification.showHtml(result.data.message+'<span class="undo tclose">关闭</span>', 3000);
            }
         }
      });
   },

   do_unsettop:function(jobs){
      if(!$.isArray(jobs)){
         jobs=[jobs];
      }

      var oldActions = [];
      var njobs = [];
      $.each(jobs,function(index,job) {
         var unsettopAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .action.unsettop');
         if(unsettopAction.length <= 0)
            return true; 

         unsettopAction.tooltip('hide'); 
         oldActions[njobs.length] = unsettopAction[0].outerHTML;
         njobs[njobs.length++] = job; 
         unsettopAction[0].outerHTML = '<img class="loading" data-action="Unsettop" src="'+ OC.imagePath('core', 'loading.gif') +'"></a>';
      });

      $.ajax({
         type: "POST",
         url: OC.filePath('jobposition', 'ajax', 'unsettop.php'),
         data: {ids:JSON.stringify(njobs)},
         dataType: 'json',
         beforeSend: function(xhr, settings) {
            $('#jobActions .unsettop').each(function(){
               if(!showLoading($(this), settings, null, {button:true}))
                  return false;
            });
         },
         success: function(result){
            if (result.status == 'success') {
               $.each(njobs,function(index,job){
                  var jobWrap = $('.jobitem').filterAttr('data-id',job);
                  if(jobWrap.data('type') == 'settop') {
                     jobWrap.remove();
                  } else {
                     /* in /All mode, change html only */
                     jobWrap.data('settop', 0).addClass('unsettop');
                     var unsettopAction = jobWrap.find('.jobactions .loading');
                     if(unsettopAction.length >0) {
                        /* now it's unsettoped. change it to to unsettoped */
                        var html = '<a href="#" class="action settop" data-action="settop" title="点击置顶">';
                        html += '</a>';
                        unsettopAction[0].outerHTML = html;
                        jobWrap.find('.jobactions .action.settop').tooltip().data('action', 'Settop');
                     }
                  }
               });
               procesSelection();
            } else {
               $.each(njobs,function(index,job) {
                  var unsettopAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .loading');
                  if(unsettopAction.length >0 && typeof(oldActions[index]) != 'undefined')
                     unsettopAction[0].outerHTML = oldActions[index];
               });
               OC.Notification.showHtml(result.data.message+'<span class="undo tclose">关闭</span>', 3000);
            }
         }
      })
   },

   do_trash:function(jobs){
      if(!$.isArray(jobs)){
         jobs=[jobs];
      }

      var oldActions = []; 
      var njobs = []; 
      $.each(jobs,function(index,job) {
         var deleteAction = $('.jobitem').filterAttr('data-id',job).data('permissions') & OC.PERMISSION_DELETE;
         if(!deleteAction)
            return true; 
         njobs[njobs.length++] =  job; 
      })

      $.ajax({
         type: "POST",
         url: OC.filePath('jobposition', 'ajax', 'delete.php'),
         data: {ids:JSON.stringify(njobs)},
         dataType: 'json',
         beforeSend: function(xhr, settings) {
            $('#jobActions .delete').each(function(){
               if(!showLoading($(this), settings, null, {button:true}))
                  return false;
            });
         },
         success: function(result){
            if (result.status == 'success') {
               if(njobs.length > 1) 
                  OC.Notification.showHtml(njobs.length + '个职位被删除进回收站。'+'<span class="undo restore">撤销</span>', 3000);
               else if(njobs.length >= 1){
                  var jobWrap = $('.jobitem').filterAttr('data-id',njobs[0]); 
                  OC.Notification.showHtml('职位<strong>' + jobWrap.data('jobtitle') + '</strong>被删除进回收站。'+'<span class="undo restore">撤销</span>', 3000);
               }
               /* remember deleted jobs */     
               JobList.trashedJobs = njobs; 
               $.each(njobs,function(index,job){
                  var jobWrap = $('.jobitem').filterAttr('data-id',job);
                  jobWrap.hide().addClass('trashed');
                  var deleteAction = jobWrap.find('.jobactions .loading');
                  if(deleteAction.length >0 && typeof(oldActions[index]) != 'undefined')
                     deleteAction[0].outerHTML = oldActions[index];

               });
               procesSelection();
            } else {
               $.each(njobs,function(index,job) {
                  var deleteAction = $('.jobitem').filterAttr('data-id',job).find('.jobactions .loading');
                  if(deleteAction.length >0 && typeof(oldActions[index]) != 'undefined')
                     deleteAction[0].outerHTML = oldActions[index];
               });
               OC.Notification.showHtml(result.data.message+'<span class="undo tclose">撤销</span>', 3000);
            }
         }
      })
   },

   do_restore: function(jobs) {
      if(!$.isArray(jobs)){
         jobs=[jobs];
      }

      var oldActions = []; 
      var njobs = []; 
      $.each(jobs,function(index,job) {
         var jobWrap = $('.jobitem').filterAttr('data-id',job); 
         /* let see if it's just trashed, and in last trashed list; we don't undo the jobs before last list */
         if(jobWrap.hasClass("trashed") && $.isArray(job, JobList.trashedJobs) != -1) {
            njobs[njobs.length++] = job;
            return true; 
         }
            
         var restoreAction = jobWrap.find('.jobactions .action.restore');
         if(restoreAction.length <= 0)
            return true; 
         //destroy restore tooltip first 
         restoreAction.tooltip('destroy');

         oldActions[njobs.length] = restoreAction[0].outerHTML;
         njobs[njobs.length++] = job;
         restoreAction[0].outerHTML = '<img class="loading" data-action="Delete" src="'+ OC.imagePath('core', 'loading.gif') +'"></a>';
      });

      $.ajax({
         type: "POST",
         url: OC.filePath('jobposition', 'ajax', 'restore.php'),
         data: {ids:JSON.stringify(njobs)},
         dataType: 'json',
         beforeSend: function(xhr, settings) {
            $('#jobActions .restore').each(function(){
               if(!showLoading($(this), settings, null, {button:true}))
                  return false;
            });
         },
         success: function(result){
            if (result.status == 'success') {
               $.each(njobs,function(index,job){
                  var jobWrap = $('.jobitem').filterAttr('data-id',job);
                  if(jobWrap.hasClass("trashed")) {
                     jobWrap.show().removeClass("trashed");
                  } else {
                     jobWrap.remove();
                  }
               });
               procesSelection();
            } else {
               $.each(njobs,function(index,job) {
                  var jobWrap = $('.jobitem').filterAttr('data-id',job);
                  if(jobWrap.hasClass("trashed"))
                     return true; 
                  var restoreAction = jobWrap.find('.jobactions .loading');
                  if(restoreAction.length >0 && typeof(oldActions[index]) != 'undefined')
                     restoreAction[0].outerHTML = oldActions[index];
               });
               OC.Notification.showHtml(result.error+'<span class="undo tclose">关闭</span>', 3000);
            }
         }
      })
   },
   
   do_infolder:function(jobs){
      if(!$.isArray(jobs)){
         jobs=[jobs];
      }

      var njobs = []; 
      $.each(jobs,function(index,job) {
         njobs[njobs.length++] =  job; 
      });

      if(njobs.length >= 1) {
         var scrolltop = $(window).scrollTop();
         $("#infolder_modal").css({top:scrolltop+75}).modal('show');
         
         $.ajax({
            type: "GET",
            url: OC.filePath('jobposition', 'ajax', 'getinfolderlist.php'),
            data: {ids:JSON.stringify(njobs)},
            beforeSend: function(xhr, settings) {
               showLoading($("#infolder_body"));
            },
            cache: false,
            success: function(result){
               $("#infolder_body").html(result);
            }
         });
      }
   },

   do_renew:function(jobs,action){
      if(!$.isArray(jobs)){
         jobs=[jobs];
      }

      var njobs = []; 
      $.each(jobs,function(index,job) {
         var renewpermission = $('.renew,.renew_success,.renew_false').filterAttr('data-jobid',job).data('permission');
         if(renewpermission == 0)
            return true; 
         njobs[njobs.length++] = job;
      });

      if(njobs.length<=0){
         var target = $('#jobActions i.renew');
         showMessagePanel('所选'+jobs.length+'个职位没有权限！', target, true, {placement:'right'});
      }else{
         $.ajax({
            type: "POST",
            url: OC.filePath('jobposition', 'ajax', 'getrenewdialog.php'),
            data: {ids:JSON.stringify(njobs),alljobis:jobs,action:action},
            dataType: 'html',
            beforeSend: function(xhr, settings) {
               $('.jobactions .renew,.renew_success,.renew_false').each(function(){
                  var target = $(this);
                  var jobid = target.data('jobid');
                  if(njobs.indexOf(jobid)>=0){
                     if(!showLoading(target, settings, null, {button:true}))
                        return false;
                  }
               });
            },
            success: function(result){
               //var scrolltop = $(window).scrollTop();
               if(action == 'renewjobs')
                  $("#refreshtip_dialog").html(result).modal();
               else if(action == 'autorenewjobs')
                  $("#autorenewdialog").html(result).modal();
            }
         });
      }
   }
};

$(document).ready(function(){
   $('#notification').hide();
   $('#notification').on('click', '.undo', function(){
      if($(this).hasClass("restore")) {
         JobList.do_restore(JobList.trashedJobs);
      }

      OC.Notification.hide();
      return false;
   });

   JobList.useUndo=(window.onbeforeunload)?true:false;
   $(window).unload(function (){
      $(window).trigger('beforeunload');
   });
   
   $(document).bind('keydown', function(event) {
      if(event.keyCode == 27) {
         $(".popup").hide();
         return true;
      }
   });

   $(document).bind('mousedown', function(e){
      e = $.event.fix(e);
      var otarget = $(e.target);
      if(otarget.closest(".popup").length>0) {
         if(otarget.hasClass("closediv") && !otarget.hasClass("jobshare")) {
            otarget.closest(".popup").hide();
            return true;
         }

         return true;
      }

      // close all popup that no need keep and dropdown by open class
      $('.popup').not('.keepopen').hide().closest('.dropdown.open').removeClass('open');

      return true;
   })

});
